package top.i51213.fastaction.printer;
import android.os.Build;
import android.print.PrintAttributes;
import android.print.PrinterCapabilitiesInfo;
import android.print.PrinterId;
import android.print.PrinterInfo;
import android.printservice.PrinterDiscoverySession;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import java.util.ArrayList;
import java.util.List;

public class MyPrinterDiscoverySession extends PrinterDiscoverySession {
    private final SharePrintService sharePrintService;

    public MyPrinterDiscoverySession(SharePrintService sharePrintService){
        this.sharePrintService=sharePrintService;
    }
    @Override
    public void onStartPrinterDiscovery(@NonNull List<PrinterId> list) {
        List<PrinterInfo> printers = this.getPrinters();
        String name = "以分享方式打印";
        PrinterInfo printerInfo = new PrinterInfo
                .Builder(sharePrintService.generatePrinterId(name), name, PrinterInfo.STATUS_IDLE)
                .build();
        printers.add(printerInfo);
        addPrinters(printers);
    }

    @Override
    public void onStopPrinterDiscovery() {

    }

    @Override
    public void onValidatePrinters(@NonNull List<PrinterId> list) {

    }

    @Override
    public void onStartPrinterStateTracking(@NonNull PrinterId printerId) {
        PrinterInfo printer = findPrinterInfo(printerId);
        if (printer != null) {
            PrinterCapabilitiesInfo capabilities =
                    new PrinterCapabilitiesInfo.Builder(printerId)
                            .setMinMargins(new PrintAttributes.Margins(200, 200, 200, 200))
                            .addMediaSize(PrintAttributes.MediaSize.ISO_A4, true)
                            .addMediaSize(PrintAttributes.MediaSize.ISO_A3, false)
                            .addResolution(new PrintAttributes.Resolution("R3", "600x600", 600, 600), true)
                            .setDuplexModes(PrintAttributes.DUPLEX_MODE_NONE, PrintAttributes.DUPLEX_MODE_NONE)
                            .setColorModes(PrintAttributes.COLOR_MODE_COLOR//
                                            | PrintAttributes.COLOR_MODE_MONOCHROME,//
                                    PrintAttributes.COLOR_MODE_MONOCHROME)
                            .build();

            printer = new PrinterInfo.Builder(printer)
                    .setCapabilities(capabilities)
                    .setStatus(PrinterInfo.STATUS_IDLE)
                    // .setDescription("fake print 1!")
                    .build();
            List<PrinterInfo> printers = new ArrayList<>();
            printers.add(printer);
            addPrinters(printers);
        }
    }

    @Override
    public void onStopPrinterStateTracking(@NonNull PrinterId printerId) {

    }

    @Override
    public void onDestroy() {

    }
    private PrinterInfo findPrinterInfo(PrinterId printerId) {
        for (PrinterInfo printer: getPrinters()){
            if (printer.getId().equals(printerId)) {
                return printer;
            }
        }
        return null;
    }
}